<template>
  <el-container class="layout">
    <!-- 一级菜单 -->
    <div class="aside-split">
      <div class="logo flx-center">
        <img src="@/assets/images/logo.png" alt="logo" />
      </div>
      <el-scrollbar>
        <div class="split-list">
          <div
            class="split-item"
            :class="{
              'split-active':
                splitActive === item.path || `/${splitActive.split('/')[1]}` === item.path,
            }"
            v-for="item in menuList"
            :key="item.path"
            @click="changeSubMenu(item)"
          >
            <el-icon>
              <component :is="item.meta.icon"></component>
            </el-icon>
            <span class="title">{{ item.meta.title }}</span>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <el-container>
      <!-- 头部 -->
      <el-header>
        <Header></Header>
      </el-header>
      <!-- 页面 -->
      <section class="main-layout-container">
        <el-aside
          :class="{ 'not-aside': !subMenu.length }"
          :style="{ width: isCollapse ? '65px' : '210px' }"
        >
          <el-scrollbar>
            <el-menu
              :default-active="activeMenu"
              :router="false"
              :collapse="isCollapse"
              :collapse-transition="false"
              :unique-opened="true"
              background-color="#ffffff"
            >
              <SubMenu :menuList="subMenu" />
            </el-menu>
          </el-scrollbar>
        </el-aside>
        <section class="main-layout-page px-3">
          <Main></Main>
        </section>
      </section>
    </el-container>
  </el-container>
</template>
<script setup>
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from '@/store'
import Main from './components/Main/Index.vue'
import SubMenu from '@/layouts/components/Menu/SubMenu.vue'
import Header from './components/Header/Index.vue'
const route = useRoute()
const router = useRouter()
const { auth, global } = useStore()

const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path))
const menuList = computed(() => auth.showMenuListGet)
const isCollapse = computed(() => global.themeConfig.isCollapse)

const subMenu = ref([])
const splitActive = ref('')
watch(
  () => [menuList, route],
  () => {
    // 当前菜单没有数据直接 return
    if (!menuList.value.length) return
    splitActive.value = route.path
    const menuItem = menuList.value.filter(
      (item) => route.path === item.path || `/${route.path.split('/')[1]}` === item.path,
    )
    if (menuItem[0].children?.length) return (subMenu.value = menuItem[0].children)
    subMenu.value = []
  },
  {
    deep: true,
    immediate: true,
  },
)

// 切换 SubMenu
const changeSubMenu = (item) => {
  splitActive.value = item.path
  if (item.children?.length) return (subMenu.value = item.children)
  subMenu.value = []
  router.push(item.path)
}
</script>
<style lang="less" scoped>
@import url('./index.less');
</style>
<style lang="less">
.el-menu {
  .el-menu-item {
    &.is-active {
      background: #f1f7ff;
    }
  }
}
</style>
